package com.prs.crm.action.archives;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.archives.KeepTime;
import com.prs.crm.service.archives.KeepTimeService;

public class KeepTimeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private KeepTime keepTime;
	private KeepTimeService keepTimeService;
	private PaginationSupport pagination;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public KeepTime getKeepTime() {
		return keepTime;
	}
	public void setKeepTime(KeepTime keepTime) {
		this.keepTime = keepTime;
	}
	public KeepTimeService getKeepTimeService() {
		return keepTimeService;
	}
	public void setKeepTimeService(KeepTimeService keepTimeService) {
		this.keepTimeService = keepTimeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	@Action("listKeepTime")
	public String listKeepTime(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(KeepTime.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getKeepTimeService().listByPage(criteria, pagination));
		return SUCCESS;	
	}
	@Action("editKeepTime")
	public String editKeepTime(){
		if (ids!=null) {
			this.keepTime=this.getKeepTimeService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action(value="saveKeepTime",results={
			@Result(name="success",type="redirect",location="listKeepTime"),
			@Result(name="error",type="chain",location="editKeepTime")
	})
	public String saveKeepTime(){
		this.getKeepTimeService().save(keepTime);
		return SUCCESS;
	}
	
	@Action(value="removeKeepTime",results={
			@Result(name="success",type="redirect",location="listKeepTime"),
			@Result(name="error",type="chain",location="listKeepTime")
	})
	public String removeKeepTime(){
		if(ids!=null){
			this.keepTime=this.getKeepTimeService().get(ids[0]);
		}
		if (keepTime.getArchivesManager().size()!=0) {
			this.addActionError("该条记录已被引用，不能删除！");
			return ERROR;
		}
		this.getKeepTimeService().batchRemove(ids);
		return SUCCESS;
	}
}
